package com.zhlb.zfba.modules.per.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhlb.zfba.common.config.Global;
import com.zhlb.zfba.common.persistance.Msg;
import com.zhlb.zfba.common.utils.DateUtil;
import com.zhlb.zfba.modules.per.bean.PoliceInfo;
import com.zhlb.zfba.modules.per.bean.PolicePathLog;
import com.zhlb.zfba.modules.per.bean.RoomInfo;
import com.zhlb.zfba.modules.per.bean.SuspectInfo;
import com.zhlb.zfba.modules.per.service.PoliceInfoService;
import com.zhlb.zfba.modules.per.service.PolicePathLogService;
import com.zhlb.zfba.modules.per.service.RoomInfoService;
import com.zhlb.zfba.modules.per.service.SuspectInfoService;

@Controller
@RequestMapping(value = "/per/policeInfo")
public class PoliceInfoController {
	@Autowired
	private PoliceInfoService policeInfoService;
	@Autowired
	private PolicePathLogService policePathLogService;
	@Autowired
	SuspectInfoService suspectInfoService;
	@Autowired
	RoomInfoService roomInfoService;
	@RequestMapping(value = {"getPolice"})
	@ResponseBody
	public PoliceInfo getPolice(PoliceInfo policeInfo) {
		PoliceInfo policeInfoEntity =policeInfoService.get(policeInfo.getInfoGUID());
		return policeInfoEntity;
	}
	@RequestMapping(value = {"test"})
	public String Test(HttpServletRequest request, HttpServletResponse response) {
		BufferedReader a;
		try {
			a = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
			String plWhere = "";
			while ((plWhere = a.readLine()) != null) {
				System.out.println(plWhere);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "";
	}
	//根据房间号查询人员信息
	@RequestMapping(value = {"query"})
	public String qeuryPersons(String roomID, Model model) {
		String msg="";
		if(StringUtils.isBlank(roomID)){
			msg="未指定房间ID";
			model.addAttribute("msg",msg);
			//model.addAttribute("roomInfo",roomInfo);
			model.addAttribute("policeInfoList",null);
			model.addAttribute("suspectInfoList",null);
			return "modules/per/policeAndSuspectList";
		}
		RoomInfo roomInfo = roomInfoService.get(roomID);
		if(roomInfo==null){
			msg="指定的房间不存在！";
		}
		PoliceInfo policeInfo =new PoliceInfo();
		policeInfo.setCurPostion(roomID);
		policeInfo.setIsLeave("0");
		List<PoliceInfo> policeInfoList=policeInfoService.findList(policeInfo);
		SuspectInfo suspectInfo =new SuspectInfo();
		suspectInfo.setCurPostion(roomID);
		suspectInfo.setIsLeave("0");
		List<SuspectInfo> suspectInfoList=suspectInfoService.findList(suspectInfo);
		model.addAttribute("msg",msg);
		model.addAttribute("roomInfo",roomInfo);
		model.addAttribute("policeInfoList",policeInfoList);
		model.addAttribute("suspectInfoList",suspectInfoList);
		return "modules/per/policeAndSuspectList";
	}
	@RequestMapping(value = {"list", ""})
	public String list(PoliceInfo policeInfo, String hisHistoryFlg,String isFirst,HttpServletRequest request, HttpServletResponse response, Model model) {
		//第一访问，默认显示当天的
		if("1".equals(isFirst)){
		String currentData=DateUtil.dateToStr(new Date(), "yyyy-MM-dd");
		  policeInfo.setInTime(currentData);
		}
		// 引入PageHepler插件
		String pageSizeStr=Global.getConfig("pagesize");
		if (!StringUtils.isNumeric(pageSizeStr)){
			pageSizeStr="10";
		}
		Integer pageSize=Integer.parseInt(pageSizeStr);
		Integer pn=0;
		if (StringUtils.isNumeric(request.getParameter("pageNo"))){
			pn=Integer.parseInt(request.getParameter("pageNo"));
		}
		//获取询问室下拉框的值
		RoomInfo roomInfo =new RoomInfo();
		roomInfo.setRoomType(4);
		List<RoomInfo> roomInfoList=roomInfoService.findList(roomInfo);
		PageHelper.startPage(pn, pageSize);
		List<PoliceInfo> list=policeInfoService.findList(policeInfo);
		PageInfo pageInfo = new PageInfo(list, 5);
		model.addAttribute("inquestRoomInfoList", roomInfoList);
		model.addAttribute("policeInfo", policeInfo);//查询条件回显
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("pageInfoJson", JSON.toJSONString(pageInfo));
		if("1".equals(hisHistoryFlg)){
			return "modules/per/policeInfoHistoryList";
		}else{
			return "modules/per/policeInfoList";
		}
	}
	@RequestMapping(value = "form")
	public String form(PoliceInfo policeInfo, Model model) {
		model.addAttribute("policeInfo", policeInfo);
		return "modules/task/policeInfo";
	}

/*	@RequestMapping(value = "save")
	public String save(PoliceInfo policeInfo, Model model, RedirectAttributes redirectAttributes) {
//		if (!beanValidator(model, policeInfo)){
//			return form(taskJobConf, model);
//		}
		policeInfoService.save(policeInfo);
		//addMessage(redirectAttributes, "保存任务计划配置成功");
		//return "redirect:"+Global.getAdminPath()+"/task/taskJobConf/?repage";
		return "";
	}*/
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Msg savePoliceInfo(PoliceInfo policeInfo, BindingResult result) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		if (result.hasErrors()) {
//			List<FieldError> errors = result.getFieldErrors();
//			for (FieldError error : errors) {
//				map.put(error.getField(), error.getDefaultMessage());
//
//			}
//			return Msg.failure().add("errorFileds", map);
//		}
		policeInfoService.save(policeInfo);
		return Msg.success();
	}
	
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(String ids) {
		policeInfoService.delete(ids);
		//addMessage(redirectAttributes, "删除任务计划配置成功");
		//return "redirect:"+Global.getAdminPath()+"/task/taskJobConf/?repage";
		return "";
	}
	@RequestMapping(value = "isLeave")
	@ResponseBody
	public Msg isLeave(PoliceInfo policeInfo) {
		policeInfoService.updateIsLeave(policeInfo);
		return Msg.success();
	}
	@RequestMapping(value = "checkPolicePathView")
	public String checkPathListofAndChart(PolicePathLog policePathLog,String isFirst, Model model) {
		if("1".equals(isFirst)){
		String currentData=DateUtil.dateToStr(new Date(), "yyyy-MM-dd");
		policePathLog.setInTime(currentData);
		}
		List<PolicePathLog> policePathLogList= policePathLogService.findList(policePathLog);
		List<PolicePathLog> policePathLogList2= policePathLogService.findList(policePathLog);
		model.addAttribute("policePathLogList", policePathLogList);
		List<PolicePathLog> policePathLogChartList=new ArrayList<PolicePathLog>();
		for(PolicePathLog temp:policePathLogList2){
			if(StringUtils.isNoneBlank(temp.getInTime())){
				int endPos=19;//
				if(temp.getInTime().length()<19){
					endPos=18;
				}
				String intimeStr=temp.getInTime().substring(11, endPos);
				temp.setInTime(intimeStr);
			}
			policePathLogChartList.add(temp);
		}
		model.addAttribute("canvasWidth",policePathLogChartList.size()*65);
		
		model.addAttribute("policePathLogListJson", JSON.toJSONString(policePathLogChartList));
		
		model.addAttribute("policePathLog", policePathLog);//2017年12月28日 查询条件回显
		return "modules/per/checkPolicePathView";
	}
}
